package com.atguigu.gulimall.product.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池配置
 */
@Configuration
//@EnableConfigurationProperties(ThreadPoolConfigProperties.class)在配置参数类上添加@Component可以省略此注解
public class MyThreadConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool){
        return new ThreadPoolExecutor(pool.getCoreSize(),//核心线程池大小
                pool.getMaxSize(),//线程池最大容量
                pool.getKeepAliveTime(),//空闲线程关闭时间
                TimeUnit.SECONDS,//时间单位
                new LinkedBlockingDeque<>(100000),//阻塞队列的长度
                Executors.defaultThreadFactory(),//使用默认线程工厂
                new ThreadPoolExecutor.AbortPolicy()//拒绝策略 -- 直接抛弃
                );

    }


}
